﻿/* ***********************************************************************************************
 * VECTOR TRANSFORMATIONS  
 * Author:  Roy Penrod  
 * Email:  taalrith@gmail.com
 * Copyright 2012-2013.  All Rights Reserved.
 * Last Modified:  01-03-2012
 * 
 * Contains useful methods to transform vectors.  All the 
 * methods are static so you don't have to create an instance
 * to call them.
 * ***********************************************************************************************
 */

using System;
using Microsoft.Xna.Framework;

namespace JumpFleetGameLib
{
    public class VectorTransformations
    {
        /// <summary>
        /// Calculates the normalized vector pointing to the given angle.  A rotation angle of 0 returns (0, -1).  To find the end point of the line, multiply the return value by the vector length and then add it to the starting point of the line. Type: Vector2
        /// </summary>
        /// <param name="rotationAngle">The angle of rotation you want the vector for.  Type: float</param>
        /// <returns>Normalized vector pointing to the given angle.  Type: Vector2</returns>
        public static Vector2 RotateVector(float rotationAngle)
        {
            Vector2 rotationVector = new Vector2((float)Math.Sin(rotationAngle), (float)Math.Cos(rotationAngle) * -1);
            rotationVector.Normalize();
            return rotationVector;
        }


        /// <summary>
        /// Used to orbit a child object around a parent object.  It returns a Vector2 representing the child object's new position point.
        /// </summary>
        /// <param name="parentPositionPoint">The point the parent object is currently positioned at.  Type: Vector2</param>
        /// <param name="childPositionPoint">The point the child object is currently positioned at.  Type: Vector2</param>
        /// <param name="rotationAngleRadians">The amount of rotation you want applied to the child object's position.  Type: float</param>
        /// <returns>The child object's new position as a point.  Type: Vector2</returns>
        public static Vector2 RotateVectorAroundParentObject(Vector2 parentPositionPoint, Vector2 childPositionPoint, float rotationAngleRadians)
        {
            // gets a vector representing the distance and direction between the child and parent
            Vector2 distanceVector = childPositionPoint - parentPositionPoint;

            // rotates the distance vector
            distanceVector = Vector2.Transform(distanceVector, Matrix.CreateRotationZ(rotationAngleRadians));

            // adds the rotated distance vector back to the parent to get the child's new position point
            return parentPositionPoint + distanceVector;
        }

    }
}
